package com.qianqian.service.model;

import com.qianqian.service.exception.BusinessError;
import lombok.Data;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Data
public class Response{

    static final   public int FLAG_SUCCESS = 1;

    static final   public int FLAG_FAIL = 0;

    private int flag = FLAG_SUCCESS;
    private String msg = "";
    private Map  data= new ConcurrentHashMap();
    private int errorCode;
    public Response(){
    }

    public Response(String msg, int flag){
        this.msg = msg;
        this.flag = flag;
    }

    public Response(String msg, int flag,int errorCode){
        this.msg = msg;
        this.flag = flag;
        this.errorCode = errorCode;
    }

    public static Response  success(String msg){
       return new Response(msg,FLAG_SUCCESS);
    }
    public static Response  success(){
        return new Response("",FLAG_SUCCESS);
    }

    public static Response success(String k,Object v){
        Response response = new Response();
        response.data.put(k,v);
        return response;
    }

    public void add(String k,Object v){
        this.data.put(k,v);
    }
    public static Response fail(String msg){
        return new Response(msg,FLAG_FAIL);
    }

    public static Response fail(int errorCode,String msg){
        return new Response(msg,FLAG_FAIL,errorCode);
    }

    public static Response fail(BusinessError businessError){
        return new Response(businessError.getMsg(),FLAG_FAIL,businessError.getErrorCode());
    }

}
